package com.taptech.doufu.ui.viewholder.personalcenter;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.cartoon.CartoonBean;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.bean.UserBean;
import com.taptech.doufu.ui.view.drawcircle.BaseRecyclerViewViewHolder;
import com.taptech.doufu.services.CartoonServices;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.TMAnalysis;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by longsiyang on 15/12/10.
 */
public class CartoonItemViewHolder<T extends CartoonBean> extends BaseRecyclerViewViewHolder implements View.OnClickListener
{

    // UI view
    protected RelativeLayout layoutGround;
    protected SimpleDraweeView coverImg;
    protected TextView cartoonTitle;
    protected TextView cartoonEpisodeCount;
    protected TextView cartoonDes;
    protected ImageView userIcon;
    protected TextView cartoonUserName;
    protected TextView cartoonScanCount;

    // Data
    T bean;


    public CartoonItemViewHolder(Context context, View itemView)
    {
        super(context, itemView);
        layoutGround = (RelativeLayout) itemView.findViewById(R.id.cartoon_layout_ground);
        coverImg = (SimpleDraweeView) itemView.findViewById(R.id.cartoon_cover_img_id);
        cartoonTitle = (TextView) itemView.findViewById(R.id.cartoon_title);
        cartoonEpisodeCount = (TextView) itemView.findViewById(R.id.cartoon_episode_count);
        cartoonDes = (TextView) itemView.findViewById(R.id.cartoon_des);
        userIcon = (ImageView) itemView.findViewById(R.id.cartoon_user_icon);
        cartoonUserName = (TextView) itemView.findViewById(R.id.cartoon_user_name);
        cartoonScanCount = (TextView) itemView.findViewById(R.id.cartoon_scan_count);

    }

    @Override
    public void setViewHolderChildViewContent(MineAbstractBean tempBean, int position)
    {
        bean = (T) tempBean;
        mPosition = position;
        if (bean == null)
        {
            return;
        }
        handleChildViewDisplay();

    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.cartoon_layout_ground:
                Map keyMap = new HashMap();
                keyMap.put("mId", bean.getId());
                keyMap.put("mPos", mPosition);
                TMAnalysis.event(mContext, "cartoon_item_click", keyMap);

                CartoonServices.enterCartoonDes(mContext, bean);
                break;
        }
    }

    protected void handleChildViewDisplay()
    {
        layoutGround.setOnClickListener(this);

        if (bean.getImage() != null)
        {
            ImageManager.loadImage(coverImg, bean.getImage(), 98 / 73.0f);
        }
        if (bean.getTitle() != null)
        {
            cartoonTitle.setText(bean.getTitle());
        }
        if (bean.getIntro() != null)
        {
            cartoonDes.setText(bean.getIntro());
        }
        if (bean.getSection_num() != null)
        {
            cartoonEpisodeCount.setText(bean.getSection_num() + "话");
        }


        if (bean.getRead_times() != null)
        {
            cartoonScanCount.setText(bean.getRead_times());
        }

        UserBean userBean = bean.getUser();

        Drawable userBitmap = mContext.getResources().getDrawable(R.drawable.default_user_portrait);
        userIcon.setImageDrawable(userBitmap);
        if (userBean != null)
        {
            if (userBean.getIcon() != null)
            {
                GlideUtil.displayCircleImage(userIcon, userBean.getIcon());
            }
            if (userBean.getName() != null)
            {
                cartoonUserName.setText(userBean.getName());
            }
        }


    }
}
